package com.tengfei.blogsystem.service.impl;

import com.tengfei.blogsystem.dao.CategoryDao;
import com.tengfei.blogsystem.pojo.Category;
import com.tengfei.blogsystem.response.ResponseResult;
import com.tengfei.blogsystem.response.ResponseState;
import com.tengfei.blogsystem.service.ICategoryService;
import com.tengfei.blogsystem.utils.Constants;
import com.tengfei.blogsystem.utils.SnowFlakeIdWorker;
import com.tengfei.blogsystem.utils.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;

/**
 * @version v1.0
 * @author} feige
 * @date 2021-10-21-15:21
 * @description
 */
@Service
@Transactional
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private SnowFlakeIdWorker idWorker;
    @Autowired
    private CategoryDao categoryDao;

    @Override
    public ResponseResult addCategory(Category category) {
        // 检查数据
        if (TextUtils.isEmpty(category.getName())) {
            return new ResponseResult(ResponseState.ERROR, "分类名称不可以为空");
        }
        if (TextUtils.isEmpty(category.getPinyin())) {
            return new ResponseResult(ResponseState.ERROR, "分类拼音名称描述不可以为空");
        }
        if (TextUtils.isEmpty(category.getDescription())) {
            return new ResponseResult(ResponseState.ERROR, "分类描述不可以为空");
        }

        category.setOrder(1);
        // 补全数据
        category.setId(String.valueOf(idWorker.nextId()));
        category.setStatus("1");
        category.setCreateTime(new Date());
        category.setUpdateTime(new Date());
        // 保存数据
        categoryDao.save(category);
        return new ResponseResult(ResponseState.SUCCESS, "添加分类成功");
    }

    @Override
    public ResponseResult getCategory(String categoryId) {
        Category categoryById = categoryDao.findOneById(categoryId);
        if (categoryById == null) {
            return new ResponseResult(ResponseState.ERROR, "分类不存在");
        }
        return new ResponseResult(ResponseState.SUCCESS, "查询分类成功", categoryById);
    }

    @Override
    public ResponseResult listCategory(int page, int size) {
        // 分页查询
        if (page < Constants.PageValue.DEFAULT_PAGE) {
            page = Constants.PageValue.DEFAULT_PAGE;
        }
        if (size < Constants.PageValue.MIN_SIZE) {
            size = Constants.PageValue.MIN_SIZE;
        }
        // 排序
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(page - 1, size, sort);
        Page<Category> categories = categoryDao.findAll(pageable);
        return new ResponseResult(ResponseState.SUCCESS, "查询分类列表成功", categories);
    }

    @Override
    public ResponseResult updateCategory(String categoryId, Category category) {
        // 找出分类信息
        Category categoryById = categoryDao.findOneById(categoryId);
        if (categoryById == null) {
            return new ResponseResult(ResponseState.ERROR, "分类不存在");
        }
        // 检查数据
        // 填充信息
        if (!TextUtils.isEmpty(category.getName())) {
            categoryById.setName(category.getName());
        }
        if (!TextUtils.isEmpty(category.getPinyin())) {
            categoryById.setPinyin(category.getPinyin());
        }
        if (!TextUtils.isEmpty(category.getDescription())) {
            categoryById.setDescription(category.getDescription());
        }
        if (category.getOrder() != 0) {
            categoryById.setOrder(category.getOrder());
        }
        // 更新update
        category.setUpdateTime(new Date());
        // 保存结果
        categoryDao.save(categoryById);
        return new ResponseResult(ResponseState.SUCCESS, "更新分类成功");
    }

    @Override
    public ResponseResult deleteCategory(String categoryId) {
        // 更新update
        int result = categoryDao.deleteByUpdateStatus(new Date(), categoryId);
        if (result > 0) {
            return new ResponseResult(ResponseState.SUCCESS, "删除分类成功");
        }
        return new ResponseResult(ResponseState.ERROR, "分类不存在");
    }
}
